package com.allen.app.api.example;

import com.allen.app.common.BaseController;
import com.allen.app.common.domain.PageResult;
import com.allen.app.common.domain.R;
import com.allen.app.domain.example.TestDemo;
import com.allen.app.service.example.TestDemoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Description:测试接口类
 * @author Limiaojun
 * @date 2021-09-13
 */
@Api(tags = "测试接口")
@RestController
@AllArgsConstructor
@RequestMapping("/test-demo")
public class TestDemoController extends BaseController {

    private final TestDemoService testDemoService;

    @PostMapping
    @ApiOperation(value = "添加测试")
    public R<Integer> add(@RequestBody TestDemo testDemo) {

        return ok(testDemoService.createTestDemo(testDemo));
    }

    @PutMapping
    @ApiOperation(value = "根据id更新测试")
    public R<Integer> edit(@RequestBody TestDemo testDemo) {

        return ok(testDemoService.updateTestDemoById(testDemo));
    }

    @DeleteMapping
    @ApiOperation(value = "根据ids删除测试")
    @ApiImplicitParam(paramType = "path", name = "ids", value = "要删除测试的ids,逗号分割", required = true, dataType = "String")
    public R<Integer> remove(@PathVariable("ids") String ids) {

        return ok(testDemoService.deleteTestDemoByIds(ids));
    }

    @ApiOperation(value = "根据id查询测试")
    @GetMapping("/{id}")
    public R<TestDemo> getInfo(@PathVariable("id") Long id) {

        return ok(testDemoService.selectTestDemoById(id));
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询测试列表")
    public R<PageResult<TestDemo>> list(TestDemo testDemo){
        startPage();
        return ok(
            PageResult.of(
                testDemoService.selectTestDemoList(testDemo)
            )
        );
    }
}
